const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const ApiKeys = sequelize.define('ApiKeys', {
  id: {
    type: DataTypes.BIGINT,
    allowNull: false,
    autoIncrement: true,
    comment: 'API密钥ID',
  },
  userId: {
    type: DataTypes.BIGINT,
    allowNull: false,
    comment: '用户ID',
  },
  name: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: 'API密钥名称',
  },
  apiKey: {
    type: DataTypes.STRING(255),
    allowNull: false,
    comment: 'API Key',
  },
  apiSecret: {
    type: DataTypes.STRING(255),
    allowNull: false,
    comment: 'API Secret',
  },
  passphrase: {
    type: DataTypes.STRING(255),
    comment: 'Passphrase',
  },
  permissions: {
    type: DataTypes.JSON,
    allowNull: false,
    comment: '权限配置',
  },
  ipWhitelist: {
    type: DataTypes.TEXT,
    comment: 'IP白名单',
  },
  status: {
    type: DataTypes.ENUM,
    defaultValue: active,
    comment: '状态',
  },
  expiresAt: {
    type: DataTypes.DATE,
    comment: '过期时间',
  },
  lastUsedAt: {
    type: DataTypes.DATE,
    comment: '最后使用时间',
  },
  deletedAt: {
    type: DataTypes.DATE,
    comment: '删除时间',
  },
  createdAt: {
    type: DataTypes.DATE,
    defaultValue: CURRENT_TIMESTAMP,
    comment: '创建时间',
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: CURRENT_TIMESTAMP,
    comment: '更新时间',
  }
}, {
  tableName: 'api_keys',
  comment: 'API密钥表',
  timestamps: true,
  paranoid: true,
  underscored: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  deletedAt: 'deleted_at'
});

module.exports = ApiKeys;
